﻿using lwzlz.Application.System.Dto;
using lwzlz.Common;
using Newtonsoft.Json;

namespace lzwlz.Web
{
    /// <summary>
    /// 用户登录验证
    /// </summary>
    public static class MallMemberLogin
    {
        public static MemberLogin MemberModel { get; set; }

        /// <summary>
        /// 写入用户信息
        /// </summary>
        /// <param name="userData"></param>
        /// <param name="minutes"></param>
        public static void SignIn(MemberLogin userData, int minutes)
        {
            // 序列化用户数据
            var data = JsonConvert.SerializeObject(userData);

            //写入Cookies
            ZCookies.WriteCookies("LWZLZ_MemberLogin", minutes * 60, data);
        }

        /// <summary>
        /// 删除用户信息
        /// </summary>
        public static void SingOut()
        {
            //写入Cookies
            ZCookies.DelCookie("LWZLZ_MemberLogin");
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        public static MemberLogin GetCurrentUser()
        {
            dynamic Model;
            try
            {
                var data = ZCookies.GetCookies("LWZLZ_MemberLogin");
                Model = JsonConvert.DeserializeObject<MemberLogin>(data);
            }
            catch
            {
                return null;
            }

            return Model;
        }

        /// <summary>
        /// 验证用户是否已登录
        /// </summary>
        /// <returns></returns>
        public static bool IsLogined()
        {
            return !string.IsNullOrEmpty(GetCurrentUser()?.UserName);
        }
    }
}